package com.atguigu.flink.chapter06.join;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter05.function.WaterSensorMapFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

public class WindowJoinDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> s1 = env.socketTextStream("hadoop102", 8888)
                .map(new WaterSensorMapFunction())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner((ws, ts) -> ws.getTs())
                );

        SingleOutputStreamOperator<WaterSensor> s2 = env.socketTextStream("hadoop102", 9999)
                .map(new WaterSensorMapFunction())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner((ws, ts) -> ws.getTs())
                );

        //窗口join  where a.id = b.id
        s1.join(s2)
                .where(WaterSensor::getId)
                .equalTo(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                /*
                泛型1 ：输入流1的类型
                泛型2 ：输入流2的类型
                泛型3 ：结果输出的类型
                 */
                .apply(new JoinFunction<WaterSensor, WaterSensor, String>() {
                    /*
                    参数1：输入流1
                    参数2 ：输入流2
                     */
                    @Override
                    public String join(WaterSensor first,
                                       WaterSensor second) throws Exception {
                        return first + "<> " + second;
                    }
                })
                .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//public class WindowJoinDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        SingleOutputStreamOperator<WaterSensor> s1 = env.socketTextStream("hadoop102", 8888)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                );
//
//        SingleOutputStreamOperator<WaterSensor> s2 = env.socketTextStream("hadoop102", 9999)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                );
//
//
//        //窗口join where a.id = b.id
//        s1.join(s2)
//                .where(WaterSensor::getId)
//                .equalTo(WaterSensor::getId)
//                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
//                /*
//                泛型1 ： 输入流1 的类型
//                泛型2 ： 输入流2 的类型
//                泛型3 ： 结果输出的类型
//                 */
//                .apply(new JoinFunction<WaterSensor, WaterSensor, String>() {
//                    /*
//                    参数1：输入流1
//                    参数2：输入流2
//                     */
//                    @Override
//                    public String join(WaterSensor first, WaterSensor second) throws Exception {
//                        return first + "<>" + second;
//                    }
//                })
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}

//
//public class WindowJoinDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//        SingleOutputStreamOperator<WaterSensor> s1 = env.socketTextStream("hadoop102", 8888)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                );
//
//        SingleOutputStreamOperator<WaterSensor> s2 = env.socketTextStream("hadoop102", 9999)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                );
//
//        //窗口join ->   where a.id = b.id
//        s1.join(s2)
//                .where(WaterSensor::getId)
//                .equalTo(WaterSensor::getId)
//                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
//                /*
//                泛型1：输入流1的类型
//                泛型2：输入流2的类型
//                泛型3：结果输出的类型
//                 */
//                .apply(new JoinFunction<WaterSensor, WaterSensor, String>() {
//                    /*
//                    参数1 ：输入流1
//                    参数2 :输入流2
//                     */
//                    @Override
//                    public String join(WaterSensor first,
//                                       WaterSensor second) throws Exception {
//                        return first + "<>" + second;
//                    }
//                })
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}